package com.foreverwin.jzymes.websocket;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.jzymes.common.domain.PageData;
import com.foreverwin.jzymes.common.mapper.CarrierScanLogAdviceMapper;
import com.foreverwin.jzymes.common.mapper.DashBoardAdviceMapper;
import com.foreverwin.jzymes.common.mapper.TranshipInfraAdviceMapper;
import com.foreverwin.jzymes.common.service.IPageDataService;
import com.foreverwin.jzymes.common.service.IProductionShiftService;
import com.foreverwin.jzymes.common.service.IResourceAdviceService;
import com.foreverwin.jzymes.common.service.ISystemRuleAdviceService;
import com.foreverwin.jzymes.production.inspect.mapper.InspectAlarmLogAdviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WebSocketService {

    @Autowired
    IProductionShiftService productionShiftService;
    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    ISystemRuleAdviceService systemRuleAdviceService;
    @Autowired
    IPageDataService pageDataService;
    @Autowired
    CarrierScanLogAdviceMapper carrierScanLogAdviceMapper;
    @Autowired
    TranshipInfraAdviceMapper transhipInfraAdviceMapper;
    @Autowired
    InspectAlarmLogAdviceMapper inspectAlarmLogAdviceMapper;
    @Autowired
    DashBoardAdviceMapper dashBoardAdviceMapper;

    /**
     * 查询页面数据
     *
     * @param site
     * @param channel
     * @param msgType
     * @return
     */
    public JSONObject findPageData(String site, String channel, String msgType ){
        Date[] dateRange = productionShiftService.findShiftRange( site, new Date() );

        PageData queryParam = new PageData();
        queryParam.setChannel( channel );
        queryParam.setMsgType( msgType );
        List<PageData> pageDataList = pageDataService.selectPageDataList( queryParam );
        if( pageDataList.size() > 0 ){
            PageData pageData = pageDataList.get( 0 );
            if( pageData.getSendTime().after( dateRange[0] ) && pageData.getSendTime().before( dateRange[1] ) ){
                return JSONObject.parseObject( pageData.getMessage() );
            }else{
                return null;
            }
        }else{
            return null;
        }
    }

    /**
     * 主动线接驳站信息
     *
     * @param transhipInfra
     */
    /*public void publishMasterTranshipInfo( TranshipInfra transhipInfra ){
        Map<String,Object> transhipMap = new HashMap<>();
        transhipMap.put( "id", transhipInfra.getResrce() );
        transhipMap.put( "rawNum", transhipInfra.getRawQty() );
        transhipMap.put( "clinkerNum", transhipInfra.getProductQty() );
        transhipMap.put( "status", resourceAdviceService.findResourceStatus( transhipInfra.getSite(), transhipInfra.getResrce() ) );

        List<Map<String,Object>> ports = new ArrayList<>();
        ports.add( transhipMap );

        Map<String,Object> valueMap = new HashMap<>();
        valueMap.put( "date", DateUtil.format( new Date(), "yyyy-MM-dd" ) );
        valueMap.put( "time", DateUtil.format( new Date(), "HH:mm" ) );
        valueMap.put( "ports", ports );

        //String meg  = ObjectMapperUtil.writeValueAsString( AjaxResult.returnSuccess().put( "value", valueMap ) );
        WebSocketServer.sendMessageToChannel( "mainLine", "WS_MASTER_TRANSHIP_INFRA", valueMap );
    }*/

}
